package com.gzx.plugin.tjzy.modular.blacklist.util;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import vip.xiaonuo.common.util.CommonHttpUtil;
import vip.xiaonuo.common.util.CommonRandomUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 黑名单工具类
 */
@Slf4j
public class BlackListUtil {

    public static void main(String[] args) {
        String resp = CommonHttpUtil.post("http://localhost:9105/api/black/verify", buildHeaders(), JSON.toJSONString(List.of("18311156132")), "黑名单校验");
        log.info("黑名单校验结果: {}", resp);
    }

    private static Map<String, Object> buildHeaders() {
        Map<String, Object> headers = new HashMap<>();
        String apiKey = "gzx";//请求Key
        String apiSecret = "zjtnb";//请求密钥
        long timestamp = System.currentTimeMillis();//当前时间戳
        headers.put("Content-Type", "application/json");
        headers.put("apiKey", apiKey);
        headers.put("apiSecret", apiSecret);
        headers.put("timestamp", String.valueOf(timestamp));
        //生成随机字符串
        String nonce = CommonRandomUtil.generateRandomString(16, true, true, true, false);
        headers.put("nonce", nonce);
        String signature = DigestUtils.md5Hex(nonce + apiKey + apiSecret + timestamp);//加密
        headers.put("signature", signature);
        return headers;
    }

}
